﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CarLeasing.Forms
{
    public partial class RegisterForm : Form
    {
        public RegisterForm()
        {
            InitializeComponent();
        }

        private void button_Login_Click(object sender, EventArgs e) => SwapToLogin?.Invoke();

        private void button_Register_Click(object sender, EventArgs e)
        {
            var errors = new[] { ErrorOnNameEmpty, ErrorOnPasswordEmpty, ErrorOnPasswordNotMatch }.Where(x => x != null).ToArray();

            if (errors.Any())
                MessageBox.Show(string.Join(Environment.NewLine, errors));
            else
                Registered?.Invoke(textBox_Name.Text, textBox_Password1.Text);
        }

        private string ErrorOnNameEmpty => string.IsNullOrWhiteSpace(textBox_Name.Text) ? "Name is empty" : null;
        private string ErrorOnPasswordEmpty => string.IsNullOrWhiteSpace(textBox_Password1.Text) ? "Password is empty" : null;
        private string ErrorOnPasswordNotMatch => textBox_Password1.Text != textBox_Password2.Text ? "Password not maching" : null;

        public event Action<string, string> Registered;
        public event Action SwapToLogin;
    }
}
